﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyHealthByDesignWebsite.Models;

namespace MyHealthByDesignWebsite.Controllers
{
    /// <summary>
    /// Description:  Controller for HealthGram
    ///Created:  Nov 2011
    ///Programmer:  Tanvi Shah
    ///pre-condition:  MyHealth information has been submitted
    ///post-condition:  HealthGram Report generation
    /// </summary>
    
    public class HealthGramController : Controller
    {
        // GET: /HealthGram/
        public ActionResult Index()
        {
            string value1 = "Good";
            string value2 = "Needs Improvement";
            string value3 = "Health Risk";
            string value4 = "Very Low";
            string value5 = "Low";
            string value6 = "High";
            string value7 = "Very High";
            string value8 = "Underweight";
            string value9 = "Normal";
            string value10 = "Overweight";
            string value11 = "Obese";
            string value12 = "Caution: Ask Doctor";
            int userID = Convert.ToInt32(Request.Cookies["UserID"].Value);

            FullDataSetDataContext db = new FullDataSetDataContext();
            User user = db.Users.Single(u => u.U_userID == userID);
            HealthProfile hp = db.HealthProfiles.Single(u => u.U_UserID == userID);
            NutritionProfile np = db.NutritionProfiles.Single(u => u.U_UserID == userID);
            HealthGram hg = new HealthGram();
            MyHealthHistoryController mhhc = new MyHealthHistoryController();

            hg.UserName = user.U_fname + " " + user.U_lname;
            hg.Sex = Convert.ToChar(user.U_sex);
            hg.Age = mhhc.getAge(userID);
            hg.Height = Convert.ToInt32(np.NP_heightFt * 12 + np.NP_heightIn);
            hg.Weight = Convert.ToInt32(np.NP_weight);

            hg.Systolic = Convert.ToInt32(hp.HP_bloodPressureLow.Value);
            if (hg.Systolic < 120)
            {
                hg.SystolicValue = value1;
                hg.SystolicColor = "#99FF66";
            }
            else if (hg.Systolic >= 120 && hg.Systolic < 140)
            {
                hg.SystolicValue = value2;
                hg.SystolicColor = "#FFFF66";
            }
            else
            {
                hg.SystolicValue = value3;
                hg.SystolicColor = "#FF5050";
            }

            hg.Diastolic = Convert.ToInt32(hp.HP_bloodPressureHigh.Value);
            if (hg.Diastolic > 80)
            {
                hg.DiastolicValue = value2;
                hg.DiastolicColor = "#FFFF66";
            }
            else
            {
                hg.DiastolicValue = value1;
                hg.DiastolicColor = "#99FF66";
            }

            hg.Waist = Convert.ToInt32(hp.HP_waistCircumference.Value);

            if (user.U_sex == 'F')
            {
                if (hg.Waist < 27.5)
                {
                    hg.WaistValue = value4;
                    hg.WaistColor = "#99FF66";
                }
                else if (hg.Waist >= 27.5 && hg.Waist < 35)
                {
                    hg.WaistValue = value5;
                    hg.WaistColor = "#99FF66";
                }
                else if (hg.Waist >= 35 && hg.Waist < 43)
                {
                    hg.WaistValue = value6;
                    hg.WaistColor = "#FFFF66";
                }
                else
                {
                    hg.WaistValue = value7;
                    hg.WaistColor = "#FF5050";
                }
            }
            else if (user.U_sex == 'M')
            {
                if (hg.Waist < 31.5)
                {
                    hg.WaistValue = value4;
                    hg.WaistColor = "#99FF66";
                }
                else if (hg.Waist >= 31.5 && hg.Waist < 39)
                {
                    hg.WaistValue = value5;
                    hg.WaistColor = "#99FF66";
                }
                else if (hg.Waist >= 39 && hg.Waist < 47)
                {
                    hg.WaistValue = value6;
                    hg.WaistColor = "#FFFF66";
                }
                else
                {
                    hg.WaistValue = value7;
                    hg.WaistColor = "#FF5050";
                }
            }

            decimal bmi = (hg.Weight * 703) / (hg.Height * hg.Height);
            hg.BMI = String.Format("{0:0.0}", bmi);
            if (Convert.ToDouble(bmi) < 18.5)
            {
                hg.BMIValue = value8;
                hg.BMIColor = "#FFFF66";
            }
            else if (Convert.ToDouble(bmi) >= 18.5 && Convert.ToDouble(bmi) < 24.9)
            {
                hg.BMIValue = value9;
                hg.BMIColor = "#99FF66";
            }
            else if (Convert.ToDouble(bmi) >= 24.9 && Convert.ToDouble(bmi) < 30)
            {
                hg.BMIValue = value10;
                hg.BMIColor = "#FFFF66";
            }
            else
            {
                hg.BMIValue = value11;
                hg.BMIColor = "#FF5050";
            }

            if (hp.HP_totalCholestrol.HasValue)
                hg.TotalCholesterol = Convert.ToInt32(hp.HP_totalCholestrol.Value);

            if (hg.TotalCholesterol < 200)
            {
                hg.TotalCholesterolValue = value1;
                hg.TotalCholesterolColor = "#99FF66";
            }
            else if (hg.TotalCholesterol >= 200 && hg.TotalCholesterol < 239)
            {
                hg.TotalCholesterolValue = value2;
                hg.TotalCholesterolColor = "#FFFF66";
            }
            else
            {
                hg.TotalCholesterolValue = value3;
                hg.TotalCholesterolColor = "#FF5050";
            }

            if (hp.HP_bloodGlucose.HasValue)
                hg.BloodGlucose = Convert.ToInt32(hp.HP_bloodGlucose.Value);

            if (hg.BloodGlucose < 100)
            {
                hg.BloodGlucoseValue = value1;
                hg.BloodGlucoseColor = "#99FF66";
            }
            else if (hg.BloodGlucose >= 100 && hg.BloodGlucose < 126)
            {
                hg.BloodGlucoseValue = value2;
                hg.BloodGlucoseColor = "#FFFF66";
            }
            else
            {
                hg.BloodGlucoseValue = value3;
                hg.BloodGlucoseColor = "#FF5050";
            }

            //hg.ActivityLevel =
            if (hp.HP_heartRate.HasValue)
                hg.HeartRate = Convert.ToInt32(hp.HP_heartRate.Value);

            if (hg.HeartRate >= 50 && hg.HeartRate <= 90)
            {
                hg.HeartRateValue = value1;
                hg.HeartRateColor = "#99FF66";
            }
            else
            {
                hg.HeartRateValue = value12;
                hg.HeartRateColor = "#FFFF66";
            }

            return View(hg);
        }
    }
}
